<!-- 护理日志 -->
<template>
    <div class="container">
        <!-- 标题 -->
        <el-header>
            <h1>护理日志</h1>
        </el-header>
        <!-- 新增输入框 -->
        <el-row class="add-log">
            <el-col :span="18">
                <el-input v-model="newLog" placeholder="请输入新的备忘录..." clearable />
            </el-col>
            <el-col :span="6">
                <el-button type="primary" @click="addLog">新增记录</el-button>
            </el-col>
        </el-row>
        <!-- 新增的内容列表 -->
        <div v-for="(logs, date) in groupedLogs" :key="date" class="log-group">
            <el-card class="log-item" v-for="(log, index) in logs" :key="index">
                <div :style="{ textDecoration: log.isCompleted ? 'line-through' : 'none' }">
                    <p>{{ log.content }}</p>
                </div>
                <el-row class="item-btn">

                    <el-button type="success" @click="markAsCompleted(log)">已完成</el-button>

                    <el-button type="danger" @click="deleteLog(log)">删除</el-button>

                </el-row>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import type { Log } from '@/API/interface/nursingLog'

// 存储所有日志的数组
const logs = ref<Log[]>([])

// 用于记录新日志的内容
const newLog = ref<string>('')

// 添加新的日志
const addLog = () => {
    if (newLog.value.trim()) {
        const date = new Date().toLocaleDateString() // 获取当前日期
        logs.value.push({ content: newLog.value, date, isCompleted: false })
        newLog.value = '' // 清空输入框
    }
}

// 标记日志为已完成
const markAsCompleted = (log: Log) => {
    log.isCompleted = true
}

// 删除日志
const deleteLog = (log: Log) => {
    const index = logs.value.indexOf(log)
    if (index > -1) {
        logs.value.splice(index, 1)
    }
}

// 按日期对日志进行分组并按完成状态排序
const groupedLogs = computed(() => {
    // 先分组，再排序
    const sortedLogs = [...logs.value].sort((a, b) => {
        // 先将未完成的日志排到前面，已完成的日志排到后面
        return a.isCompleted === b.isCompleted ? 0 : a.isCompleted ? 1 : -1
    })

    // 对日志按日期分组
    return sortedLogs.reduce((groups: Record<string, Log[]>, log) => {
        const date = log.date
        if (!groups[date]) {
            groups[date] = []
        }
        groups[date].push(log)
        return groups
    }, {})
})

</script>

<style scoped lang="less">
@primary-color: #409EFF;
@background-color: #f4f4f4;
@border-color: #ccc;

.container {
    padding: 20px;
    font-family: Arial, sans-serif;
}

.el-header {
    background-color: @primary-color;
    color: white;
    text-align: center;
    line-height: 30px;
    border-radius: 5px;
    height: 30px;
}

.add-log {
    margin-top: 20px;
}

.el-input {
    width: 100%;
    border-radius: 5px;
}

.el-button {
    width: 100%;
    height: 100%;
}

.log-group {
    margin-top: 20px;
}

.log-item {
    margin-bottom: 15px;
    background: #fff;
    border-radius: 5px;

    p {
        margin-bottom: 3px;
        font-size: 17px;
    }

    .item-btn {
        display: flex;
        justify-content: right;

        * {
            width: 60px;
        }
    }

}

.el-card /deep/ .el-card__body {
    padding: 10px;
}
</style>